<%--
  Created by IntelliJ IDEA.
  User: ADMIN
  Date: 2023/3/27
  Time: 20:47
  To change this template use File | Settings | File Templates.
--%>
<%--项目绝对路径--%>

<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="java.lang.reflect.Field" %>
<%@ page import="java.sql.*" %>
<%@ page import="java.io.InputStream" %>
<%@ page import="java.io.FileInputStream" %>
<%@ page import="java.io.BufferedInputStream" %>
<%@ page import="java.util.*" %>
<%@ page import="java.util.Date" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<html>
<head>
    <title>做登入和退出</title>
</head>
<body>
<!--处理登入跳转-->
<%

    request.setCharacterEncoding("utf-8");
    //state = 111 登入 222退出

Integer state = Integer.valueOf(request.getParameter("state"));
    System.out.println("state === "+state);
    String  username = null;
    String password = null;
    String depart = null;
    String sql = null;

    //做退出操作 删除session的所有值/跳到首页
    if(state == 222){

        session.removeAttribute("username");
        session.removeAttribute("depart");
        session.removeAttribute("id");
        session.removeAttribute("avatar");
        out.print("<script>alert('退出成功');location.href='/'</script>");
    }

    if(state == 111){
        username = request.getParameter("username");
        password = request.getParameter("password");
        depart = request.getParameter("depart");
       sql = "select * from user where username ='"+username+"'and password = '"+password+"' and depart ="+depart;


        //jdbc 操作
        String url = "jdbc:mysql://localhost:3306/carxianyu";

        String user = "root";
        String pwd = "root";
        ResultSet rs =null;
        Connection conn=null;
        try {
            Class.forName("com.mysql.jdbc.Driver");//加载驱动
            conn = DriverManager.getConnection("jdbc:mysql://43.142.239.28:13307/carxianyu?useUnicode=true&characterEncoding=UTF-8&serverTimeZone=UTC","root","root");
            PreparedStatement a = conn.prepareStatement(sql);
            rs = a.executeQuery();
            System.out.println("执行了sql =====》"+sql);


            /*
             * 登入成功后  session中存放数据,同时跳转到 对应的界面
             * */

            while (rs.next()){

                session.setAttribute("id",rs.getString("id"));
                session.setAttribute("username",rs.getString("username"));
                session.setAttribute("avatar",rs.getString("avatar"));
                session.setAttribute("depart",depart);


                //如果是 管理员
                if(11 == Integer.valueOf(depart)){
                    out.print("<script>alert('管理员登录成功');location.href='main.jsp?username="+username+"'</script>");

                }else{

                    out.print("<script>alert('用户登录成功');location.href='/'</script>");

                }

            }
            //如果没有数据就会跳转到登入界面
            out.print("<script>alert('账号、密码、身份错误');location.href='login.jsp'</script>");
        }catch (Exception exception){}finally {
            rs.close();
            conn.close();
        }

    }








%>







</body>
</html>
